package com.guoj.rabbitmq.handler;

import com.guoj.rabbitmq.entity.ResponseEntity;
import com.guoj.rabbitmq.entity.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	@ExceptionHandler(value = RuntimeException.class)
	@ResponseBody
	public ResponseEntity exceptionHandler(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
		logger.error("======================================= 全局异常信息捕获 ======================================");
		logger.error("全局异常信息捕获：", ex);
		return new ResponseEntity(false, StatusCode.ERROR, "发生异常", ex.getMessage());
	}
}
